using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Text;

using Kenly.DBFramework.Gateway;

namespace Kenly.DBFramework.Reflection
{
    /// <summary>
    /// 
    /// </summary>
    public static class ChangedProperty
    {
        /// <summary>
        /// Gets the FieldName of ChangedPropertyNames in TableGateway.
        /// </summary>
        public static readonly string ContainerFieldName = "_changedPropertyNames";

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static List<string> Find(object entity)
        {
            List<string> changedPropertyNames = new List<string>();
            if (entity == null)
            {
                return changedPropertyNames;
            }
            Type tableGatewayBaseType = entity.GetType().BaseType;
            if(tableGatewayBaseType==null)
            {
                return changedPropertyNames;
            }

            FieldInfo changedPropertyNamesFieldInfo = tableGatewayBaseType.GetField(
                ChangedProperty.ContainerFieldName, BindingFlags.NonPublic | BindingFlags.Instance);
            
            if (changedPropertyNamesFieldInfo != null)
            {
                changedPropertyNames = changedPropertyNamesFieldInfo.GetValue(entity) as List<string>;
            }
            return (changedPropertyNames!=null)?changedPropertyNames:new List<string>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        //public static List<string> GetChangedPropertyNames(object entity)
        //{
        //    return Find(entity);
        //}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        public static void Clear(object entity)
        {
            ChangedProperty.Find(entity).Clear();
        }

    }
}
